<?php
$page='main_forum';
include_once 'header.php';
require_once '../connect/connect.php';
$tbl_name="forum_answer"; 


// Get value of id that sent from hidden field
extract($_POST);
$id=$_POST['id'];

// Find highest answer number. 
$sql="SELECT MAX(a_id) AS Maxa_id FROM $tbl_name WHERE question_id='$id'";
$result=mysql_query($sql);
$rows=mysql_fetch_assoc($result);

// add + 1 to highest answer number and keep it in variable name "$Max_id". if there no answer yet set it = 1 
if ($rows) {
$Max_id = $rows['Maxa_id']+1;
}
else {
$Max_id = 1;
}

// get values that sent from form 
//$a_name=$_POST['a_name'];
//$a_email=$_POST['a_email'];
$a_answer=$_POST['a_answer']; 

$datetime=date("d/m/y H:i:s"); // create date and time

// Insert answer 
$sql2="INSERT INTO $tbl_name(question_id, a_id,  a_answer, a_datetime)VALUES('$id', '$Max_id',  '$a_answer', '$datetime')";
$result2=mysql_query($sql2)or die("Error executing '$sql2':" . mysql_error());

if($result2){
header("location:view_topic.php?id=$id");
}
else {
echo "ERROR";
}

mysql_close();
 include_once 'footer.php';
?>

